package com.example.administrator.testproject.download.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.example.administrator.testproject.R;
import com.example.administrator.testproject.download.DownState;
import com.example.administrator.testproject.download.HttpLiteDownManager;
import com.example.administrator.testproject.download.bean.LiteOrmDownInfo;
import com.example.administrator.testproject.download.listener.HttpDownOnNextListener;

import java.util.List;

/**
 * Created by shengfh on 2017/12/26.
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private Context mContent;
    private List<LiteOrmDownInfo> mListData;
    private HttpLiteDownManager manager;
    public RecyclerAdapter(Context context,List<LiteOrmDownInfo> mListData) {
        this.mContent=context;
        this.mListData = mListData;
        manager= HttpLiteDownManager.getInstance();
    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerAdapter.ViewHolder(LayoutInflater.from(mContent).inflate(R.layout.view_item_holder,null));
    }

    @Override
    public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, int position) {
        final LiteOrmDownInfo downInfo = mListData.get(position);
//        holder.setData(downInfo);
        downInfo.setListener(new HttpDownOnNextListener<LiteOrmDownInfo>() {
            @Override
            public void onNext(LiteOrmDownInfo baseDownEntity) {
                holder.tvMsg.setText("提示：下载完成");
                Toast.makeText(mContent,baseDownEntity.getSavePath(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStart() {
                holder.tvMsg.setText("提示:开始下载");
            }

            @Override
            public void onComplete() {
                holder.tvMsg.setText("提示：下载结束");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                holder.tvMsg.setText("失败:"+e.toString());
                Log.e("Tag",e.getMessage());
            }


            @Override
            public void onPuase() {
                super.onPuase();
                holder.tvMsg.setText("提示:暂停");
            }

            @Override
            public void onStop() {
                super.onStop();
            }

            @Override
            public void updateProgress(long readLength, long countLength) {
                holder.tvMsg.setText("提示:下载中");
                holder.progressBar.setMax((int) countLength);
                holder.progressBar.setProgress((int) readLength);
            }
        });
        holder.progressBar.setMax((int) downInfo.getCountLength());
        holder.progressBar.setProgress((int) downInfo.getReadLength());
        /*第一次恢复 */
        switch (downInfo.getState()){
            case START:
                /*起始状态*/
                break;
            case PAUSE:
                holder.tvMsg.setText("暂停中");
                break;
            case DOWN:
                manager.startDown(downInfo);
                break;
            case STOP:
                holder.tvMsg.setText("下载停止");
                break;
            case ERROR:
                holder.tvMsg.setText("下載錯誤");
                break;
            case  FINISH:
                holder.tvMsg.setText("下载完成");
                break;
        }
        holder.down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(downInfo.getState()!= DownState.FINISH){
                    manager.startDown(downInfo);
                }
            }
        });
        holder.pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.pause(downInfo);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private final Button down;
        private final Button pause;
        private final TextView tvMsg;
        private NumberProgressBar progressBar;
        public ViewHolder(View view) {
            super(view);
            down = (Button)view.findViewById(R.id.btn_rx_down);
            pause = (Button)view.findViewById(R.id.btn_rx_pause);
            progressBar = (NumberProgressBar)view.findViewById(R.id.number_progress_bar);
            tvMsg = (TextView)view.findViewById(R.id.tv_msg);
        }
    }
}
